package com.longjianghuasheng.logistics.business.mine.fragment

import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.business.freight.FleetOrEnterpriseFragment
import com.longjianghuasheng.logistics.business.freight.PersonalCooperationFragment
import com.longjianghuasheng.logistics.business.mine.viewmodel.UserInfoViewModel
import com.longjianghuasheng.logistics.databinding.FragmentUserInfoBinding
import com.longjianghuasheng.logistics.eventViewModel

class UserInfoFragment : BaseFragment<UserInfoViewModel, FragmentUserInfoBinding>() {
    private val titles by lazy {
        arrayOf("个人", "企业")
    }

    private val fragments by lazy {
        arrayListOf<Fragment>()
    }

    private val userInfoPagerAdapter by lazy {
        UserInfoPagerAdapter(this)
    }


    override fun layoutId(): Int = R.layout.fragment_user_info

    override fun initView(savedInstanceState: Bundle?) {
        initTitle()
        initTabAndViewPager()
    }

    private fun initTabAndViewPager() {

        mDatabind.run {


            titles.forEach {

                tabLayout.addTab(tabLayout.newTab().setText(it))


            }

//            fragments.add(PersonalInformationFragment())
            fragments.add(PersonalCooperationFragment())
//            fragments.add(EnterpriseInfoFragment())
            fragments.add(FleetOrEnterpriseFragment.newInstance(true))

            viewPager.run {


                offscreenPageLimit = titles.size

                adapter = userInfoPagerAdapter



                TabLayoutMediator(tabLayout, this) { tab, position ->
                    tab.text = titles[position]


                }.attach()

            }

        }
    }

    @SuppressLint("NewApi", "UseCompatTextViewDrawableApis")
    private fun initTitle() {
        mDatabind.include.titleBar.run {

            leftText.compoundDrawableTintList = ColorStateList.valueOf(Color.WHITE)

            setTitle("个人信息").centerText.typeface = Typeface.defaultFromStyle(Typeface.BOLD)

            setLeftClickListener {
                navigateUp()
            }
        }


    }

    override fun createObserver() {
        super.createObserver()
        eventViewModel.onGoBackToMyEvent.observeInFragment(this) {
            navigateUp()
        }
    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }

    inner class UserInfoPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {


        override fun getItemCount(): Int = fragments.size

        override fun createFragment(position: Int): Fragment = fragments[position]

    }
}